home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / uucico / send.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-20  |  40.4 KB  |  1,388 lines

  1. /* send.c
  2.    Routines to send a file.
  3.  
  4.    Copyright (C) 1991, 1992, 1993, 1994, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char send_rcsid[] = "$Id: send.c,v 1.51 1995/06/21 19:15:49 ian Rel $";
  30. #endif
  31.  
  32. #include <errno.h>
  33.  
  34. #include "uudefs.h"
  35. #include "uuconf.h"
  36. #include "system.h"
  37. #include "prot.h"
  38. #include "trans.h"
  39.  
  40. /* We keep this information in the pinfo field of the stransfer
  41.    structure.  */
  42. struct ssendinfo
  43. {
  44.   /* Local user to send mail to (may be NULL).  */
  45.   char *zmail;
  46.   /* Full file name.  */
  47.   char *zfile;
  48.   /* Number of bytes in file.  */
  49.   long cbytes;
  50.   /* TRUE if this was a local request.  */
  51.   boolean flocal;
  52.   /* TRUE if this is a spool directory file.  */
  53.   boolean fspool;
  54.   /* TRUE if the file has been completely sent.  */
  55.   boolean fsent;
  56.   /* TRUE if the file send will never succeed; used by
  57.      flocal_send_cancelled.  */
  58.   boolean fnever;
  59.   /* Execution file for sending an unsupported E request.  */
  60.   char *zexec;
  61.   /* Confirmation command received in fsend_await_confirm.  */
  62.   char *zconfirm;
  63. };
  64.  
  65. /* Local functions.  */
  66.  
  67. static void usfree_send P((struct stransfer *qtrans));
  68. static boolean flocal_send_fail P((struct scmd *qcmd,
  69.                    struct sdaemon *qdaemon,
  70.                    const char *zwhy));
  71. static boolean flocal_send_request P((struct stransfer *qtrans,
  72.                       struct sdaemon *qdaemon));
  73. static boolean flocal_send_await_reply P((struct stransfer *qtrans,
  74.                       struct sdaemon *qdaemon,
  75.                       const char *zdata, size_t cdata));
  76. static boolean flocal_send_cancelled P((struct stransfer *qtrans,
  77.                     struct sdaemon *qdaemon));
  78. static boolean flocal_send_open_file P((struct stransfer *qtrans,
  79.                     struct sdaemon *qdaemon));
  80. static boolean fremote_rec_fail P((struct sdaemon *qdaemon,
  81.                    enum tfailure twhy, int iremote));
  82. static boolean fremote_rec_fail_send P((struct stransfer *qtrans,
  83.                     struct sdaemon *qdaemon));
  84. static boolean fremote_rec_reply P((struct stransfer *qtrans,
  85.                     struct sdaemon *qdaemon));
  86. static boolean fsend_file_end P((struct stransfer *qtrans,
  87.                  struct sdaemon *qdaemon));
  88. static boolean fsend_await_confirm P((struct stransfer *qtrans,
  89.                       struct sdaemon *qdaemon,
  90.                       const char *zdata, size_t cdata));
  91. static boolean fsend_exec_file_init P((struct stransfer *qtrans,
  92.                        struct sdaemon *qdaemon));
  93. static void usadd_exec_line P((char **pz, size_t *pcalc, size_t *pclen,
  94.                    int bcmd, const char *z1, const char *z2));
  95. static boolean fsend_exec_file P((struct stransfer *qtrans,
  96.                   struct sdaemon *qdaemon));
  97.  
  98. /* Free up a send stransfer structure.  */
  99.  
  100. static void
  101. usfree_send (qtrans)
  102.      struct stransfer *qtrans;
  103. {
  104.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  105.  
  106.   if (qinfo != NULL)
  107.     {
  108.       ubuffree (qinfo->zmail);
  109.       ubuffree (qinfo->zfile);
  110.       ubuffree (qinfo->zexec);
  111.       ubuffree (qinfo->zconfirm);
  112.       xfree (qtrans->pinfo);
  113.     }
  114.  
  115.   utransfree (qtrans);
  116. }      
  117.  
  118. /* Set up a local request to send a file.  This may be called before
  119.    we have even tried to call the remote system.
  120.  
  121.    If we are using a traditional protocol, which doesn't support
  122.    channel numbers and doesn't permit the file to be sent until an
  123.    acknowledgement has been received, the sequence of function calls
  124.    looks like this:
  125.  
  126.    flocal_send_file_init --> fqueue_local
  127.    flocal_send_request (sends S request) --> fqueue_receive
  128.    flocal_send_await_reply (waits for SY) --> fqueue_send
  129.    flocal_send_open_file (opens file, calls pffile) --> fqueue_send
  130.    send file
  131.    fsend_file_end (calls pffile) --> fqueue_receive
  132.    fsend_await_confirm (waits for CY)
  133.  
  134.    If flocal_send_await_reply gets an SN, it deletes the request.  If
  135.    the SY reply contains a file position at which to start sending,
  136.    flocal_send_await_reply sets qinfo->ipos.
  137.  
  138.    This gets more complex if the protocol supports channels.  In that
  139.    case, we want to start sending the file data immediately, to avoid
  140.    the round trip delay between flocal_send_request and
  141.    flocal_send_await_reply.  To do this, flocal_send_request calls
  142.    fqueue_send rather than fqueue_receive.  The main execution
  143.    sequence looks like this:
  144.  
  145.    flocal_send_file_init --> fqueue_local
  146.    flocal_send_request (sends S request) --> fqueue_send
  147.    flocal_send_open_file (opens file, calls pffile) --> fqueue_send
  148.    send file
  149.    fsend_file_end (calls pffile) --> fqueue_receive
  150.    sometime: flocal_send_await_reply (waits for SY)
  151.    fsend_await_confirm (waits for CY)
  152.  
  153.    In this case flocal_send_await_reply must be run before
  154.    fsend_await_confirm; it may be run anytime after
  155.    flocal_send_request.
  156.  
  157.    If flocal_send_await_reply is called before the entire file has
  158.    been sent: if it gets an SN, it sets the file position to the end
  159.    and arranges to call flocal_send_cancelled.  If it gets a file
  160.    position request, it must adjust the file position accordingly.
  161.  
  162.    If flocal_send_await_reply is called after the entire file has been
  163.    sent: if it gets an SN, it can simply delete the request.  It can
  164.    ignore any file position request.
  165.  
  166.    If the request is not deleted, flocal_send_await_reply must arrange
  167.    for the next string to be passed to fsend_await_confirm.
  168.    Presumably fsend_await_confirm will only be called after the entire
  169.    file has been sent.
  170.  
  171.    Just to make things even more complex, these same routines support
  172.    sending execution requests, since that is much like sending a file.
  173.    For an execution request, the bcmd character will be E rather than
  174.    S.  If an execution request is being sent to a system which does
  175.    not support them, it must be sent as two S requests instead.  The
  176.    second one will be the execution file, but no actual file is
  177.    created; instead the zexec and znext fields in the ssendinfo
  178.    structure are used.  So if the bcmd character is E, then if the
  179.    zexec field is NULL, the data file is being sent, otherwise the
  180.    fake execution file is being sent.  */
  181.  
  182. boolean
  183. flocal_send_file_init (qdaemon, qcmd)
  184.      struct sdaemon *qdaemon;
  185.      struct scmd *qcmd;
  186. {
  187.   const struct uuconf_system *qsys;
  188.   boolean fspool;
  189.   char *zfile;
  190.   long cbytes;
  191.   struct ssendinfo *qinfo;
  192.   struct stransfer *qtrans;
  193.  
  194.   qsys = qdaemon->qsys;
  195.  
  196.   if (qdaemon->fcaller
  197.       ? ! qsys->uuconf_fcall_transfer
  198.       : ! qsys->uuconf_fcalled_transfer)
  199.     {
  200.       /* uux or uucp should have already made sure that the transfer
  201.      is possible, but it might have changed since then.  */
  202.       if (! qsys->uuconf_fcall_transfer
  203.       && ! qsys->uuconf_fcalled_transfer)
  204.     return flocal_send_fail (qcmd, qdaemon,
  205.                  "not permitted to transfer files");
  206.  
  207.       /* We can't do the request now, but it may get done later.  */
  208.       return TRUE;
  209.     }
  210.  
  211.   /* The 'C' option means that the file has been copied to the spool
  212.      directory.  */
  213.   if (strchr (qcmd->zoptions, 'C') == NULL
  214.       && ! fspool_file (qcmd->zfrom))
  215.     {
  216.       fspool = FALSE;
  217.       if (! fin_directory_list (qcmd->zfrom,
  218.                 qsys->uuconf_pzlocal_send,
  219.                 qsys->uuconf_zpubdir, TRUE,
  220.                 TRUE, qcmd->zuser))
  221.     return flocal_send_fail (qcmd, qdaemon, "not permitted to send");
  222.       zfile = zbufcpy (qcmd->zfrom);
  223.     }
  224.   else
  225.     {
  226.       fspool = TRUE;
  227.       zfile = zsysdep_spool_file_name (qsys, qcmd->ztemp, qcmd->pseq);
  228.       if (zfile == NULL)
  229.     return FALSE;
  230.     }
  231.  
  232.   /* Make sure we meet any local size restrictions.  The connection
  233.      may not have been opened at this point, so we can't check remote
  234.      size restrictions.  */
  235.   cbytes = csysdep_size (zfile);
  236.   if (cbytes < 0)
  237.     {
  238.       ubuffree (zfile);
  239.       if (cbytes != -1)
  240.     return flocal_send_fail (qcmd, qdaemon, "can not get size");
  241.       /* A cbytes value of -1 means that the file does not exist.
  242.      This can happen legitimately if it has already been sent from
  243.      the spool directory.  */
  244.       if (! fspool)
  245.     return flocal_send_fail (qcmd, qdaemon, "does not exist");
  246.       (void) fsysdep_did_work (qcmd->pseq);
  247.       return TRUE;
  248.     }
  249.  
  250.   if (qdaemon->clocal_size != -1
  251.       && qdaemon->clocal_size < cbytes)
  252.     {
  253.       ubuffree (zfile);
  254.  
  255.       if (qdaemon->cmax_ever == -2)
  256.     {
  257.       long c1, c2;
  258.  
  259.       c1 = cmax_size_ever (qsys->uuconf_qcall_local_size);
  260.       c2 = cmax_size_ever (qsys->uuconf_qcalled_local_size);
  261.       if (c1 > c2)
  262.         qdaemon->cmax_ever = c1;
  263.       else
  264.         qdaemon->cmax_ever = c2;
  265.     }
  266.               
  267.       if (qdaemon->cmax_ever != -1
  268.       && qdaemon->cmax_ever < qcmd->cbytes)
  269.     return flocal_send_fail (qcmd, qdaemon, "too large to send");
  270.  
  271.       return TRUE;
  272.     }
  273.  
  274.   /* We are now prepared to send the command to the remote system.  We
  275.      queue up a transfer request to send the command when we are
  276.      ready.  */
  277.   qinfo = (struct ssendinfo *) xmalloc (sizeof (struct ssendinfo));
  278.   if (strchr (qcmd->zoptions, 'm') == NULL)
  279.     qinfo->zmail = NULL;
  280.   else
  281.     qinfo->zmail = zbufcpy (qcmd->zuser);
  282.   qinfo->zfile = zfile;
  283.   qinfo->cbytes = cbytes;
  284.   qinfo->flocal = strchr (qcmd->zuser, '!') == NULL;
  285.   qinfo->fspool = fspool;
  286.   qinfo->fsent = FALSE;
  287.   qinfo->zexec = NULL;
  288.   qinfo->zconfirm = NULL;
  289.  
  290.   qtrans = qtransalc (qcmd);
  291.   qtrans->psendfn = flocal_send_request;
  292.   qtrans->pinfo = (pointer) qinfo;
  293.  
  294.   return fqueue_local (qdaemon, qtrans);
  295. }
  296.  
  297. /* Clean up after a failing local send request.  If zwhy is not NULL,
  298.    this reports an error to the log file and to the user.  */
  299.  
  300. static boolean
  301. flocal_send_fail (qcmd, qdaemon, zwhy)
  302.      struct scmd *qcmd;
  303.      struct sdaemon *qdaemon;
  304.      const char *zwhy;
  305. {
  306.   if (zwhy != NULL)
  307.     {
  308.       const char *zfrom;
  309.       char *zfree;
  310.       const char *ztemp;
  311.  
  312.       if (qcmd->bcmd != 'E')
  313.     {
  314.       zfrom = qcmd->zfrom;
  315.       zfree = NULL;
  316.     }
  317.       else
  318.     {
  319.       zfree = zbufalc (strlen (qcmd->zfrom)
  320.                + sizeof " (execution of \"\")"
  321.                + strlen (qcmd->zcmd));
  322.       sprintf (zfree, "%s (execution of \"%s\")", qcmd->zfrom,
  323.            qcmd->zcmd);
  324.       zfrom = zfree;
  325.     }
  326.  
  327.       ulog (LOG_ERROR, "%s: %s", zfrom, zwhy);
  328.  
  329.       /* We only save the temporary file if this is a request from the
  330.      local system; otherwise a remote system could launch a denial
  331.      of service attack by filling up the .Preserve directory
  332.      (local users have much simpler methods for this type of
  333.      denial of service attack, so there is little point to using a
  334.      more sophisticated scheme).  */
  335.       if (strchr (qcmd->zuser, '!') == NULL)
  336.     ztemp = zsysdep_save_temp_file (qcmd->pseq);
  337.       else
  338.     ztemp = NULL;
  339.       (void) fmail_transfer (FALSE, qcmd->zuser, (const char *) NULL,
  340.                  zwhy, zfrom, (const char *) NULL,
  341.                  qcmd->zto, qdaemon->qsys->uuconf_zname, ztemp);
  342.  
  343.       ubuffree (zfree);
  344.     }
  345.  
  346.   (void) fsysdep_did_work (qcmd->pseq);
  347.  
  348.   return TRUE;
  349. }
  350.  
  351. /* This is called when we are ready to send the request to the remote
  352.    system.  We form the request and send it over.  If the protocol
  353.    does not support multiple channels, we start waiting for the
  354.    response; otherwise we can start sending the file immediately.  */
  355.  
  356. static boolean
  357. flocal_send_request (qtrans, qdaemon)
  358.      struct stransfer *qtrans;
  359.      struct sdaemon *qdaemon;
  360. {
  361.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  362.   char *zsend;
  363.   const char *znotify;
  364.   char absize[20];
  365.   boolean fret;
  366.  
  367.   /* Make sure the file meets any remote size restrictions.  */
  368.   if (qdaemon->cmax_receive != -1
  369.       && qdaemon->cmax_receive < qinfo->cbytes)
  370.     {
  371.       fret = flocal_send_fail (&qtrans->s, qdaemon, "too large for receiver");
  372.       usfree_send (qtrans);
  373.       return fret;
  374.     }
  375.  
  376.   /* Make sure the file still exists--it may have been removed between
  377.      the conversation startup and now.  After we have sent over the S
  378.      command we must give an error if we can't find the file.  */
  379.   if (! fsysdep_file_exists (qinfo->zfile))
  380.     {
  381.       (void) fsysdep_did_work (qtrans->s.pseq);
  382.       usfree_send (qtrans);
  383.       return TRUE;
  384.     }
  385.  
  386.   /* If we are using a protocol which can make multiple channels, then
  387.      we can open and send the file whenever we are ready.  This is
  388.      because we will be able to distinguish the response by the
  389.      channel it is directed to.  This assumes that every protocol
  390.      which supports multiple channels also supports sending the file
  391.      position in mid-stream, since otherwise we would not be able to
  392.      restart files.  */
  393.   qtrans->fcmd = TRUE;
  394.   qtrans->psendfn = flocal_send_open_file;
  395.   qtrans->precfn = flocal_send_await_reply;
  396.  
  397.   if (qdaemon->cchans > 1)
  398.     fret = fqueue_send (qdaemon, qtrans);
  399.   else
  400.     fret = fqueue_receive (qdaemon, qtrans);
  401.   if (! fret)
  402.     return FALSE;
  403.  
  404.   /* Construct the notify string to send.  If we are going to send a
  405.      size or an execution command, it must be non-empty.  */
  406.   znotify = qtrans->s.znotify;
  407.   if (znotify == NULL)
  408.     znotify = "";
  409.   if ((qdaemon->ifeatures & FEATURE_SIZES) != 0
  410.       || (qtrans->s.bcmd == 'E'
  411.       && (qdaemon->ifeatures & FEATURE_EXEC) != 0))
  412.     {
  413.       if (*znotify == '\0')
  414.     znotify = "\"\"";
  415.     }
  416.   else
  417.     {
  418.       /* We don't need a notify string.  Some crufty UUCP code can't
  419.      handle a pair of double quotes.  */
  420.       if (strcmp (znotify, "\"\"") == 0)
  421.     znotify = "";
  422.     }
  423.  
  424.   /* Construct the size string to send.  */
  425.   if ((qdaemon->ifeatures & FEATURE_SIZES) == 0
  426.       && (qtrans->s.bcmd != 'E'
  427.       || (qdaemon->ifeatures & FEATURE_EXEC) == 0))
  428.     absize[0] = '\0';
  429.   else if ((qdaemon->ifeatures & FEATURE_V103) == 0)
  430.     sprintf (absize, "0x%lx", (unsigned long) qinfo->cbytes);
  431.   else
  432.     sprintf (absize, "%ld", qinfo->cbytes);
  433.  
  434.   zsend = zbufalc (strlen (qtrans->s.zfrom) + strlen (qtrans->s.zto)
  435.            + strlen (qtrans->s.zuser) + strlen (qtrans->s.zoptions)
  436.            + strlen (qtrans->s.ztemp) + strlen (znotify)
  437.            + strlen (absize)
  438.            + (qtrans->s.zcmd != NULL ? strlen (qtrans->s.zcmd) : 0)
  439.            + 50);
  440.  
  441.   /* If this an execution request and the other side supports
  442.      execution requests, we send an E command.  Otherwise we send an S
  443.      command.  The case of an execution request when we are sending
  444.      the fake execution file is handled just like an S request at this
  445.      point.  */
  446.   if (qtrans->s.bcmd == 'E'
  447.       && (qdaemon->ifeatures & FEATURE_EXEC) != 0)
  448.     {
  449.       /* Send the string
  450.      E zfrom zto zuser zoptions ztemp imode znotify size zcmd
  451.      to the remote system.  We put a '-' in front of the (possibly
  452.      empty) options and a '0' in front of the mode.  */
  453.       sprintf (zsend, "E %s %s %s -%s %s 0%o %s %s %s", qtrans->s.zfrom,
  454.            qtrans->s.zto, qtrans->s.zuser, qtrans->s.zoptions,
  455.            qtrans->s.ztemp, qtrans->s.imode, znotify, absize,
  456.            qtrans->s.zcmd);
  457.     }
  458.   else
  459.     {
  460.       const char *zoptions, *zdummy;
  461.  
  462.       /* Send the string
  463.      S zfrom zto zuser zoptions ztemp imode znotify
  464.      to the remote system.  We put a '-' in front of the (possibly
  465.      empty) options and a '0' in front of the mode.  If size
  466.      negotiation is supported, we also send the size; in this case
  467.      if znotify is empty we must send it as "".  If this is really
  468.      an execution request, we have to simplify the options string
  469.      to remove the various execution options which may confuse the
  470.      remote system.  SVR4 expects a string "dummy" between the
  471.      notify string and the size; I don't know why.  */
  472.       if (qtrans->s.bcmd != 'E')
  473.     zoptions = qtrans->s.zoptions;
  474.       else if (strchr (qtrans->s.zoptions, 'C') != NULL)
  475.     {
  476.       /* This should set zoptions to "C", but at least one UUCP
  477.          program gets confused by it.  That means that it will
  478.          fail in certain cases, but I suppose we might as well
  479.          kowtow to compatibility.  This shouldn't matter to any
  480.          other program, I hope.  */
  481.       zoptions = "";
  482.     }
  483.       else
  484.     zoptions = "c";
  485.  
  486.       if ((qdaemon->ifeatures & FEATURE_SVR4) != 0)
  487.     zdummy = " dummy ";
  488.       else
  489.     zdummy = " ";
  490.  
  491.       sprintf (zsend, "S %s %s %s -%s %s 0%o %s%s%s", qtrans->s.zfrom,
  492.            qtrans->s.zto, qtrans->s.zuser, zoptions,
  493.            qtrans->s.ztemp, qtrans->s.imode, znotify, zdummy,
  494.            absize);
  495.     }
  496.  
  497.   fret = (*qdaemon->qproto->pfsendcmd) (qdaemon, zsend, qtrans->ilocal,
  498.                     qtrans->iremote);
  499.   ubuffree (zsend);
  500.  
  501.   /* If fret is FALSE, we should free qtrans here, but see the comment
  502.      at the end of flocal_rec_send_request.  */
  503.  
  504.   return fret;
  505. }
  506.  
  507. /* This is called when a reply is received for the send request.  As
  508.    described at length above, if the protocol supports multiple
  509.    channels we may be in the middle of sending the file, or we may
  510.    even finished sending the file.  */
  511.  
  512. static boolean
  513. flocal_send_await_reply (qtrans, qdaemon, zdata, cdata)
  514.      struct stransfer *qtrans;
  515.      struct sdaemon *qdaemon;
  516.      const char *zdata;
  517.      size_t cdata;
  518. {
  519.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  520.   char bcmd;
  521.  
  522.   if (qtrans->s.bcmd == 'E'
  523.       && (qdaemon->ifeatures & FEATURE_EXEC) != 0)
  524.     bcmd = 'E';
  525.   else
  526.     bcmd = 'S';
  527.   if (zdata[0] != bcmd
  528.       || (zdata[1] != 'Y' && zdata[1] != 'N'))
  529.     {
  530.       ulog (LOG_ERROR, "%s: Bad response to %c request: \"%s\"",
  531.         qtrans->s.zfrom, bcmd, zdata);
  532.       usfree_send (qtrans);
  533.       return FALSE;
  534.     }
  535.  
  536.   if (zdata[1] == 'N')
  537.     {
  538.       const char *zerr;
  539.       boolean fnever;
  540.  
  541.       fnever = TRUE;
  542.       if (zdata[2] == '2')
  543.     zerr = "permission denied by remote";
  544.       else if (zdata[2] == '4')
  545.     {
  546.       zerr = "remote cannot create work files";
  547.       fnever = FALSE;
  548.     }
  549.       else if (zdata[2] == '6')
  550.     {
  551.       zerr = "too large for remote now";
  552.       fnever = FALSE;
  553.     }
  554.       else if (zdata[2] == '7')
  555.     {
  556.       /* The file is too large to ever send.  */
  557.       zerr = "too large for remote";
  558.     }
  559.       else if (zdata[2] == '8')
  560.     {
  561.       /* The file was already received by the remote system.  This
  562.          is not an error, it just means that the ack from the
  563.          remote was lost in the previous conversation, and there
  564.          is no need to resend the file.  */
  565.       zerr = NULL;
  566.     }
  567.       else if (zdata[2] == '9')
  568.     {
  569.       /* Remote has run out of channels.  */
  570.       zerr = "too many channels for remote";
  571.       fnever = FALSE;
  572.  
  573.       /* Drop one channel; using exactly one channel causes
  574.          slightly different behahaviour in a few places, so don't
  575.          decrement to one.  */
  576.       if (qdaemon->cchans > 2)
  577.         --qdaemon->cchans;
  578.     }
  579.       else
  580.     zerr = "unknown reason";
  581.  
  582.       if (! fnever
  583.       || (qtrans->s.bcmd == 'E'
  584.           && (qdaemon->ifeatures & FEATURE_EXEC) == 0
  585.           && qinfo->zexec == NULL))
  586.     {
  587.       if (qtrans->s.bcmd == 'E')
  588.         ulog (LOG_ERROR, "%s (execution of \"%s\"): %s",
  589.           qtrans->s.zfrom, qtrans->s.zcmd, zerr);
  590.       else
  591.         ulog (LOG_ERROR, "%s: %s", qtrans->s.zfrom, zerr);
  592.     }
  593.       else
  594.     {
  595.       if (! flocal_send_fail (&qtrans->s, qdaemon, zerr))
  596.         return FALSE;
  597.     }
  598.  
  599.       /* If the protocol does not support multiple channels, we can
  600.      simply remove the transaction.  Otherwise we must make sure
  601.      the remote side knows that we have finished sending the file
  602.      data.  If we have already sent the entire file, there will be
  603.      no confusion.  */
  604.       if (qdaemon->cchans == 1 || qinfo->fsent)
  605.     {
  606.       /* If we are breaking a 'E' command into two 'S' commands,
  607.          and that was for the first 'S' command, we still have to
  608.          send the second one.  */
  609.       if (fnever
  610.           && qtrans->s.bcmd == 'E'
  611.           && (qdaemon->ifeatures & FEATURE_EXEC) == 0
  612.           && qinfo->zexec == NULL)
  613.         return fsend_exec_file_init (qtrans, qdaemon);
  614.  
  615.       usfree_send (qtrans);
  616.       return TRUE;
  617.     }
  618.       else
  619.     {
  620.       /* Seek to the end of the file so that the next read will
  621.          send end of file.  We have to be careful here, because we
  622.          may have actually already sent end of file--we could be
  623.          being called because of data received while the end of
  624.          file block was sent.  */
  625.       if (! ffileseekend (qtrans->e))
  626.         {
  627.           ulog (LOG_ERROR, "seek to end: %s", strerror (errno));
  628.           usfree_send (qtrans);
  629.           return FALSE;
  630.         }
  631.       qtrans->psendfn = flocal_send_cancelled;
  632.       qtrans->precfn = NULL;
  633.  
  634.       qinfo->fnever = fnever;
  635.  
  636.       return fqueue_send (qdaemon, qtrans);
  637.     }
  638.     }
  639.  
  640.   /* A number following the SY or EY is the file position to start
  641.      sending from.  If we are already sending the file, we must set
  642.      the position accordingly.  */
  643.   if (zdata[2] != '\0')
  644.     {
  645.       long cskip;
  646.  
  647.       cskip = strtol ((char *) (zdata + 2), (char **) NULL, 0);
  648.       if (cskip > 0 && qtrans->ipos < cskip)
  649.     {
  650.       if (qtrans->fsendfile && ! qinfo->fsent)
  651.         {
  652.           if (! ffileseek (qtrans->e, cskip))
  653.         {
  654.           ulog (LOG_ERROR, "seek: %s", strerror (errno));
  655.           usfree_send (qtrans);
  656.           return FALSE;
  657.         }
  658.         }
  659.       qtrans->ipos = cskip;
  660.     }
  661.     }
  662.  
  663.   /* Now queue up to send the file or to wait for the confirmation.
  664.      We already set psendfn at the end of flocal_send_request.  If the
  665.      protocol supports multiple channels, we have already called
  666.      fqueue_send; calling it again would move the request in the
  667.      queue, which would make the log file a bit confusing.  */
  668.   qtrans->fcmd = TRUE;
  669.   qtrans->precfn = fsend_await_confirm;
  670.   if (qinfo->fsent)
  671.     return fqueue_receive (qdaemon, qtrans);
  672.   else if (qdaemon->cchans <= 1)
  673.     return fqueue_send (qdaemon, qtrans);
  674.   else
  675.     return TRUE;
  676. }
  677.  
  678. /* Open the file, if any, and prepare to send it.  */
  679.  
  680. static boolean
  681. flocal_send_open_file (qtrans, qdaemon)
  682.      struct stransfer *qtrans;
  683.      struct sdaemon *qdaemon;
  684. {
  685.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  686.   const char *zuser;
  687.  
  688.   /* If this is not a fake execution file, open it.  */
  689.   if (qinfo->zexec == NULL)
  690.     {
  691.       /* If there is an ! in the user name, this is a remote request
  692.      queued up by fremote_xcmd_init.  */
  693.       zuser = qtrans->s.zuser;
  694.       if (strchr (zuser, '!') != NULL)
  695.     zuser = NULL;
  696.  
  697.       qtrans->e = esysdep_open_send (qdaemon->qsys, qinfo->zfile,
  698.                      ! qinfo->fspool, zuser);
  699.       if (! ffileisopen (qtrans->e))
  700.     {
  701.       (void) fmail_transfer (FALSE, qtrans->s.zuser,
  702.                  (const char *) NULL,
  703.                  "cannot open file",
  704.                  qtrans->s.zfrom, (const char *) NULL,
  705.                  qtrans->s.zto,
  706.                  qdaemon->qsys->uuconf_zname,
  707.                  (qinfo->flocal
  708.                   ? zsysdep_save_temp_file (qtrans->s.pseq)
  709.                   : (const char *) NULL));
  710.       (void) fsysdep_did_work (qtrans->s.pseq);
  711.       usfree_send (qtrans);
  712.  
  713.       /* Unfortunately, there is no way to cancel a file send
  714.          after we've already put it in progress.  So we have to
  715.          return FALSE to drop the connection.  */
  716.       return FALSE;
  717.     }
  718.     }
  719.  
  720.   /* If flocal_send_await_reply has received a reply with a file
  721.      position, it will have set qtrans->ipos to the position at which
  722.      to start.  */
  723.   if (qtrans->ipos > 0)
  724.     {
  725.       if (qinfo->zexec != NULL)
  726.     {
  727.       if (qtrans->ipos > qtrans->cbytes)
  728.         qtrans->ipos = qtrans->cbytes;
  729.     }
  730.       else
  731.     {
  732.       if (! ffileseek (qtrans->e, qtrans->ipos))
  733.         {
  734.           ulog (LOG_ERROR, "seek: %s", strerror (errno));
  735.           usfree_send (qtrans);
  736.           return FALSE;
  737.         }
  738.     }
  739.     }
  740.  
  741.   /* We don't bother to log sending the execution file.  */
  742.   if (qinfo->zexec == NULL)
  743.     {
  744.       const char *zsend;
  745.       char *zalc;
  746.  
  747.       if (qtrans->s.bcmd != 'E')
  748.     {
  749.       zsend = qtrans->s.zfrom;
  750.       zalc = NULL;
  751.     }
  752.       else
  753.     {
  754.       zalc = zbufalc (strlen (qtrans->s.zcmd) + sizeof " ()"
  755.               + strlen (qtrans->s.zfrom));
  756.       sprintf (zalc, "%s (%s)", qtrans->s.zcmd, qtrans->s.zfrom);
  757.       zsend = zalc;
  758.     }
  759.  
  760.       qtrans->zlog = zbufalc (sizeof "Sending ( bytes resume at )"
  761.                   + strlen (zsend) + 50);
  762.       sprintf (qtrans->zlog, "Sending %s (%ld bytes", zsend, qinfo->cbytes);
  763.       if (qtrans->ipos > 0)
  764.     sprintf (qtrans->zlog + strlen (qtrans->zlog), " resume at %ld",
  765.          qtrans->ipos);
  766.       strcat (qtrans->zlog, ")");
  767.  
  768.       ubuffree (zalc);
  769.     }
  770.  
  771.   if (qdaemon->qproto->pffile != NULL)
  772.     {
  773.       boolean fhandled;
  774.  
  775.       if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, TRUE, TRUE,
  776.                     qinfo->cbytes - qtrans->ipos,
  777.                     &fhandled))
  778.     {
  779.       usfree_send (qtrans);
  780.       return FALSE;
  781.     }
  782.  
  783.       if (fhandled)
  784.     return TRUE;
  785.     }
  786.  
  787.   if (qinfo->zexec != NULL)
  788.     qtrans->psendfn = fsend_exec_file;
  789.   else
  790.     {
  791.       qtrans->fsendfile = TRUE;
  792.       qtrans->psendfn = fsend_file_end;
  793.     }
  794.  
  795.   return fqueue_send (qdaemon, qtrans);
  796. }
  797.  
  798. /* Cancel a file send.  This is only called for a protocol which
  799.    supports multiple channels.  It is needed so that both systems
  800.    agree as to when a channel is no longer needed.  */
  801.  
  802. static boolean
  803. flocal_send_cancelled (qtrans, qdaemon)
  804.      struct stransfer *qtrans;
  805.      struct sdaemon *qdaemon;
  806. {
  807.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  808.  
  809.   /* If we are breaking a 'E' command into two 'S' commands, and that
  810.      was for the first 'S' command, and the first 'S' command will
  811.      never be sent, we still have to send the second one.  */
  812.   if (qinfo->fnever
  813.       && qtrans->s.bcmd == 'E'
  814.       && (qdaemon->ifeatures & FEATURE_EXEC) == 0
  815.       && qinfo->zexec == NULL)
  816.     return fsend_exec_file_init (qtrans, qdaemon);
  817.  
  818.   usfree_send (qtrans);
  819.   return TRUE;
  820. }
  821.  
  822. /* A remote request to receive a file (meaning that we have to send a
  823.    file).  The sequence of functions calls is as follows:
  824.  
  825.    fremote_rec_file_init (open file) --> fqueue_remote
  826.    fremote_rec_reply (send RY, call pffile) --> fqueue_send
  827.    send file
  828.    fsend_file_end (calls pffile) --> fqueue_receive
  829.    fsend_await_confirm (waits for CY)
  830.    */
  831.  
  832. boolean
  833. fremote_rec_file_init (qdaemon, qcmd, iremote)
  834.      struct sdaemon *qdaemon;
  835.      struct scmd *qcmd;
  836.      int iremote;
  837. {
  838.   const struct uuconf_system *qsys;
  839.   char *zfile;
  840.   boolean fbadname;
  841.   long cbytes;
  842.   unsigned int imode;
  843.   openfile_t e;
  844.   struct ssendinfo *qinfo;
  845.   struct stransfer *qtrans;
  846.  
  847.   qsys = qdaemon->qsys;
  848.  
  849.   if (! qsys->uuconf_fsend_request)
  850.     {
  851.       ulog (LOG_ERROR, "%s: not permitted to send files to remote",
  852.         qcmd->zfrom);
  853.       return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
  854.     }
  855.  
  856.   if (fspool_file (qcmd->zfrom))
  857.     {
  858.       ulog (LOG_ERROR, "%s: not permitted to send", qcmd->zfrom);
  859.       return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
  860.     }
  861.  
  862.   zfile = zsysdep_local_file (qcmd->zfrom, qsys->uuconf_zpubdir, &fbadname);
  863.   if (zfile == NULL && fbadname)
  864.     {
  865.       ulog (LOG_ERROR, "%s: bad local file name", qcmd->zfrom);
  866.       return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
  867.     }
  868.   if (zfile != NULL)
  869.     {
  870.       char *zbased;
  871.  
  872.       zbased = zsysdep_add_base (zfile, qcmd->zto);
  873.       ubuffree (zfile);
  874.       zfile = zbased;
  875.     }
  876.   if (zfile == NULL)
  877.     return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
  878.  
  879.   if (! fin_directory_list (zfile, qsys->uuconf_pzremote_send,
  880.                 qsys->uuconf_zpubdir, TRUE, TRUE,
  881.                 (const char *) NULL))
  882.     {
  883.       ulog (LOG_ERROR, "%s: not permitted to send", zfile);
  884.       ubuffree (zfile);
  885.       return fremote_rec_fail (qdaemon, FAILURE_PERM, iremote);
  886.     }
  887.  
  888.   /* If the file is larger than the amount of space the other side
  889.      reported, we can't send it.  Should we adjust this check based on
  890.      the restart position?  */
  891.   cbytes = csysdep_size (zfile);
  892.   if (cbytes != -1
  893.       && ((qcmd->cbytes != -1 && qcmd->cbytes < cbytes)
  894.       || (qdaemon->cremote_size != -1
  895.           && qdaemon->cremote_size < cbytes)
  896.       || (qdaemon->cmax_receive != -1
  897.           && qdaemon->cmax_receive < cbytes)))
  898.     {
  899.       ulog (LOG_ERROR, "%s: too large to send", zfile);
  900.       ubuffree (zfile);
  901.       return fremote_rec_fail (qdaemon, FAILURE_SIZE, iremote);
  902.     }
  903.  
  904.   imode = ixsysdep_file_mode (zfile);
  905.  
  906.   e = esysdep_open_send (qsys, zfile, TRUE, (const char *) NULL);
  907.   if (! ffileisopen (e))
  908.     {
  909.       ubuffree (zfile);
  910.       return fremote_rec_fail (qdaemon, FAILURE_OPEN, iremote);
  911.     }
  912.  
  913.   /* If the remote requested that the file send start from a
  914.      particular position, arrange to do so.  */
  915.   if (qcmd->ipos > 0)
  916.     {
  917.       if (! ffileseek (e, qcmd->ipos))
  918.     {
  919.       ulog (LOG_ERROR, "seek: %s", strerror (errno));
  920.       ubuffree (zfile);
  921.       return FALSE;
  922.     }
  923.     }
  924.  
  925.   qinfo = (struct ssendinfo *) xmalloc (sizeof (struct ssendinfo));
  926.   qinfo->zmail = NULL;
  927.   qinfo->zfile = zfile;
  928.   qinfo->cbytes = cbytes;
  929.   qinfo->flocal = FALSE;
  930.   qinfo->fspool = FALSE;
  931.   qinfo->fsent = FALSE;
  932.   qinfo->zexec = NULL;
  933.   qinfo->zconfirm = NULL;
  934.  
  935.   qtrans = qtransalc (qcmd);
  936.   qtrans->psendfn = fremote_rec_reply;
  937.   qtrans->iremote = iremote;
  938.   qtrans->pinfo = (pointer) qinfo;
  939.   qtrans->e = e;
  940.   qtrans->ipos = qcmd->ipos;
  941.   qtrans->s.imode = imode;
  942.  
  943.   return fqueue_remote (qdaemon, qtrans);
  944. }
  945.  
  946. /* Reply to a receive request from the remote system, and prepare to
  947.    start sending the file.  */
  948.  
  949. static boolean
  950. fremote_rec_reply (qtrans, qdaemon)
  951.      struct stransfer *qtrans;
  952.      struct sdaemon *qdaemon;
  953. {
  954.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  955.   char absend[50];
  956.  
  957.   qtrans->fsendfile = TRUE;
  958.   qtrans->psendfn = fsend_file_end;
  959.   qtrans->fcmd = TRUE;
  960.   qtrans->precfn = fsend_await_confirm;
  961.  
  962.   if (! fqueue_send (qdaemon, qtrans))
  963.     return FALSE;
  964.  
  965.   qtrans->zlog = zbufalc (sizeof "Sending ( bytes) "
  966.               + strlen (qtrans->s.zfrom) + 25);
  967.   sprintf (qtrans->zlog, "Sending %s (%ld bytes)", qtrans->s.zfrom,
  968.        qinfo->cbytes);
  969.  
  970.   /* We send the file size because SVR4 UUCP does.  We don't look for
  971.      it.  We send a trailing M if we want to request a hangup.  We
  972.      send it both after the mode and at the end of the entire string;
  973.      I don't know where programs look for it.  */
  974.   if (qdaemon->frequest_hangup)
  975.     DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
  976.             "fremote_rec_reply: Requesting remote to transfer control");
  977.   sprintf (absend, "RY 0%o%s 0x%lx%s", qtrans->s.imode,
  978.        qdaemon->frequest_hangup ? "M" : "",
  979.        (unsigned long) qinfo->cbytes,
  980.        qdaemon->frequest_hangup ? "M" : "");
  981.   if (! (*qdaemon->qproto->pfsendcmd) (qdaemon, absend, qtrans->ilocal,
  982.                        qtrans->iremote))
  983.     {
  984.       (void) ffileclose (qtrans->e);
  985.       /* Should probably free qtrans here, but see the comment at the
  986.          end of flocal_rec_send_request.  */
  987.       return FALSE;
  988.     }
  989.  
  990.   if (qdaemon->qproto->pffile != NULL)
  991.     {
  992.       boolean fhandled;
  993.  
  994.       if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, TRUE, TRUE,
  995.                     qinfo->cbytes, &fhandled))
  996.     {
  997.       usfree_send (qtrans);
  998.       return FALSE;
  999.     }
  1000.     }
  1001.  
  1002.   return TRUE;
  1003. }
  1004.  
  1005. /* If we can't send a file as requested by the remote system, queue up
  1006.    a failure reply which will be sent when possible.  */
  1007.  
  1008. static boolean
  1009. fremote_rec_fail (qdaemon, twhy, iremote)
  1010.      struct sdaemon *qdaemon;
  1011.      enum tfailure twhy;
  1012.      int iremote;
  1013. {
  1014.   enum tfailure *ptinfo;
  1015.   struct stransfer *qtrans;
  1016.  
  1017.   ptinfo = (enum tfailure *) xmalloc (sizeof (enum tfailure));
  1018.   *ptinfo = twhy;
  1019.  
  1020.   qtrans = qtransalc ((struct scmd *) NULL);
  1021.   qtrans->psendfn = fremote_rec_fail_send;
  1022.   qtrans->iremote = iremote;
  1023.   qtrans->pinfo = (pointer) ptinfo;
  1024.  
  1025.   return fqueue_remote (qdaemon, qtrans);
  1026. }
  1027.  
  1028. /* Send a failure string for a receive command to the remote system;
  1029.    this is called when we are ready to reply to the command.  */
  1030.  
  1031. static boolean
  1032. fremote_rec_fail_send (qtrans, qdaemon)
  1033.      struct stransfer *qtrans;
  1034.      struct sdaemon *qdaemon;
  1035. {
  1036.   enum tfailure *ptinfo = (enum tfailure *) qtrans->pinfo;
  1037.   const char *z;
  1038.   int ilocal, iremote;
  1039.  
  1040.   switch (*ptinfo)
  1041.     {
  1042.     case FAILURE_PERM:
  1043.     case FAILURE_OPEN:
  1044.       z = "RN2";
  1045.       break;
  1046.     case FAILURE_SIZE:
  1047.       z = "RN6";
  1048.       break;
  1049.     default:
  1050.       z = "RN";
  1051.       break;
  1052.     }
  1053.   
  1054.   ilocal = qtrans->ilocal;
  1055.   iremote = qtrans->iremote;
  1056.  
  1057.   xfree (qtrans->pinfo);
  1058.   utransfree (qtrans);
  1059.  
  1060.   return (*qdaemon->qproto->pfsendcmd) (qdaemon, z, ilocal, iremote);
  1061. }
  1062.  
  1063. /* This is called when the main loop has finished sending a file.  It
  1064.    prepares to wait for a response from the remote system.  Note that
  1065.    if this is a local request and the protocol supports multiple
  1066.    channels, we may not even have received a confirmation of the send
  1067.    request.  */
  1068.  
  1069. static boolean
  1070. fsend_file_end (qtrans, qdaemon)
  1071.      struct stransfer *qtrans;
  1072.      struct sdaemon *qdaemon;
  1073. {
  1074.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  1075.  
  1076.   if (qdaemon->qproto->pffile != NULL)
  1077.     {
  1078.       boolean fhandled;
  1079.  
  1080.       if (! (*qdaemon->qproto->pffile) (qdaemon, qtrans, FALSE, TRUE,
  1081.                     (long) -1, &fhandled))
  1082.     {
  1083.       usfree_send (qtrans);
  1084.       return FALSE;
  1085.     }
  1086.  
  1087.       if (fhandled)
  1088.     return TRUE;
  1089.     }
  1090.  
  1091.   qinfo->fsent = TRUE;
  1092.  
  1093.   /* If zconfirm is set, then we have already received the
  1094.      confirmation, and should call fsend_await_confirm directly.  */
  1095.   if (qinfo->zconfirm != NULL)
  1096.     return fsend_await_confirm (qtrans, qdaemon, qinfo->zconfirm,
  1097.                 strlen (qinfo->zconfirm) + 1);
  1098.  
  1099.   /* qtrans->precfn should have been set by a previous function.  */
  1100.   return fqueue_receive (qdaemon, qtrans);
  1101. }
  1102.  
  1103. /* Handle the confirmation string received after sending a file.  */
  1104.  
  1105. /*ARGSUSED*/
  1106. static boolean
  1107. fsend_await_confirm (qtrans, qdaemon, zdata, cdata)
  1108.      struct stransfer *qtrans;
  1109.      struct sdaemon *qdaemon;
  1110.      const char *zdata;
  1111.      size_t cdata;
  1112. {
  1113.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  1114.   boolean fnever;
  1115.   const char *zerr;
  1116.  
  1117.   /* If fsent is FALSE, it means that we have received the
  1118.      confirmation before fsend_file_end got called.  To avoid
  1119.      confusion, we save away the confirmation message, and let
  1120.      fsend_file_end call us directly.  If we did not do this, we would
  1121.      have to fix a thorny race condition in floop, which wants to
  1122.      refer to the qtrans structure after sending the end of the file.  */
  1123.   if (! qinfo->fsent)
  1124.     {
  1125.       qinfo->zconfirm = zbufcpy (zdata);
  1126.       return TRUE;
  1127.     }
  1128.  
  1129.   if (qinfo->zexec == NULL)
  1130.     (void) ffileclose (qtrans->e);
  1131.  
  1132.   fnever = FALSE;
  1133.   if (zdata[0] != 'C'
  1134.       || (zdata[1] != 'Y' && zdata[1] != 'N'))
  1135.     {
  1136.       zerr = "bad confirmation from remote";
  1137.       ulog (LOG_ERROR, "%s: %s \"%s\"", qtrans->s.zfrom, zerr, zdata);
  1138.     }
  1139.   else if (zdata[1] == 'N')
  1140.     {
  1141.       fnever = TRUE;
  1142.       if (zdata[2] == '5')
  1143.     {
  1144.       zerr = "file could not be stored in final location";
  1145.       ulog (LOG_ERROR, "%s: %s", qtrans->s.zfrom, zerr);
  1146.     }
  1147.       else
  1148.     {
  1149.       zerr = "file send failed for unknown reason";
  1150.       ulog (LOG_ERROR, "%s: %s \"%s\"", qtrans->s.zfrom, zerr, zdata);
  1151.     }
  1152.     }
  1153.   else
  1154.     {
  1155.       zerr = NULL;
  1156.  
  1157.       /* If we receive CYM, it means that the other side wants us to
  1158.      hang up so that they can send us something.  The
  1159.      fhangup_requested field is checked in the main loop.  */
  1160.       if (zdata[2] == 'M' && qdaemon->fmaster)
  1161.     {
  1162.       DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
  1163.               "fsend_await_confirm: Remote has requested transfer of control");
  1164.       qdaemon->fhangup_requested = TRUE;
  1165.     }
  1166.     }
  1167.  
  1168.   ustats (zerr == NULL, qtrans->s.zuser, qdaemon->qsys->uuconf_zname,
  1169.       TRUE, qtrans->cbytes, qtrans->isecs, qtrans->imicros,
  1170.       qdaemon->fcaller);
  1171.   qdaemon->csent += qtrans->cbytes;
  1172.  
  1173.   if (zerr == NULL)
  1174.     {
  1175.       /* If this is an execution request, and the remote system
  1176.      doesn't support execution requests, we have to set up the
  1177.      fake execution file and loop around again.  */
  1178.       if (qtrans->s.bcmd == 'E'
  1179.       && (qdaemon->ifeatures & FEATURE_EXEC) == 0
  1180.       && qinfo->zexec == NULL)
  1181.     return fsend_exec_file_init (qtrans, qdaemon);
  1182.  
  1183.       /* Send mail about the transfer if requested.  */
  1184.       if (qinfo->zmail != NULL && *qinfo->zmail != '\0')
  1185.     (void) fmail_transfer (TRUE, qtrans->s.zuser, qinfo->zmail,
  1186.                    (const char *) NULL,
  1187.                    qtrans->s.zfrom, (const char *) NULL,
  1188.                    qtrans->s.zto, qdaemon->qsys->uuconf_zname,
  1189.                    (const char *) NULL);
  1190.  
  1191.       if (qtrans->s.pseq != NULL)
  1192.     (void) fsysdep_did_work (qtrans->s.pseq);
  1193.     }
  1194.   else
  1195.     {
  1196.       /* If the file send failed, we only try to save the file and
  1197.      send mail if it was requested locally and it will never
  1198.      succeed.  We send mail to qinfo->zmail if set, otherwise to
  1199.      qtrans->s.zuser.  I hope this is reasonable.  */
  1200.       if (fnever && qinfo->flocal)
  1201.     {
  1202.       (void) fmail_transfer (FALSE, qtrans->s.zuser, qinfo->zmail,
  1203.                  zerr, qtrans->s.zfrom, (const char *) NULL,
  1204.                  qtrans->s.zto, qdaemon->qsys->uuconf_zname,
  1205.                  zsysdep_save_temp_file (qtrans->s.pseq));
  1206.       (void) fsysdep_did_work (qtrans->s.pseq);
  1207.     }
  1208.     }
  1209.  
  1210.   usfree_send (qtrans);
  1211.  
  1212.   return TRUE;
  1213. }
  1214.  
  1215. /* Prepare to send an execution file to a system which does not
  1216.    support execution requests.  We build the execution file in memory,
  1217.    and then call flocal_send_request as though we were sending a real
  1218.    file.  Instead of sending a file, the code in flocal_send_open_file
  1219.    will arrange to call fsend_exec_file which will send data out of
  1220.    the buffer we have created.  */
  1221.  
  1222. static boolean
  1223. fsend_exec_file_init (qtrans, qdaemon)
  1224.      struct stransfer *qtrans;
  1225.      struct sdaemon *qdaemon;
  1226. {
  1227.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  1228.   char *zxqtfile;
  1229.   char abtname[CFILE_NAME_LEN];
  1230.   char abxname[CFILE_NAME_LEN];
  1231.   char *z;
  1232.   size_t calc, clen;
  1233.  
  1234.   z = NULL;
  1235.   calc = 0;
  1236.   clen = 0;
  1237.  
  1238.   usadd_exec_line (&z, &calc, &clen, 'U', qtrans->s.zuser,
  1239.            qdaemon->zlocalname);
  1240.   usadd_exec_line (&z, &calc, &clen, 'F', qtrans->s.zto, "");
  1241.   usadd_exec_line (&z, &calc, &clen, 'I', qtrans->s.zto, "");
  1242.   if (strchr (qtrans->s.zoptions, 'N') != NULL)
  1243.     usadd_exec_line (&z, &calc, &clen, 'N', "", "");
  1244.   if (strchr (qtrans->s.zoptions, 'Z') != NULL)
  1245.     usadd_exec_line (&z, &calc, &clen, 'Z', "", "");
  1246.   if (strchr (qtrans->s.zoptions, 'R') != NULL)
  1247.     usadd_exec_line (&z, &calc, &clen, 'R', qtrans->s.znotify, "");
  1248.   if (strchr (qtrans->s.zoptions, 'e') != NULL)
  1249.     usadd_exec_line (&z, &calc, &clen, 'e', "", "");
  1250.   usadd_exec_line (&z, &calc, &clen, 'C', qtrans->s.zcmd, "");
  1251.  
  1252.   qinfo->zexec = z;
  1253.   qinfo->cbytes = clen;
  1254.  
  1255.   zxqtfile = zsysdep_data_file_name (qdaemon->qsys, qdaemon->zlocalname,
  1256.                      BDEFAULT_UUX_GRADE, TRUE, abtname,
  1257.                      (char *) NULL, abxname);
  1258.   if (zxqtfile == NULL)
  1259.     {
  1260.       usfree_send (qtrans);
  1261.       return FALSE;
  1262.     }
  1263.   ubuffree (zxqtfile);
  1264.  
  1265.   ubuffree ((char *) qtrans->s.zfrom);
  1266.   qtrans->s.zfrom = zbufcpy (abtname);
  1267.   ubuffree ((char *) qtrans->s.zto);
  1268.   qtrans->s.zto = zbufcpy (abxname);
  1269.   ubuffree ((char *) qtrans->s.zoptions);
  1270.   qtrans->s.zoptions = zbufcpy ("C");
  1271.   ubuffree ((char *) qtrans->s.ztemp);
  1272.   qtrans->s.ztemp = zbufcpy (abtname);
  1273.  
  1274.   qtrans->psendfn = flocal_send_request;
  1275.   qtrans->precfn = NULL;
  1276.   qtrans->ipos = 0;
  1277.   qtrans->cbytes = 0;
  1278.   qtrans->isecs = 0;
  1279.   qtrans->imicros = 0;
  1280.   qinfo->fsent = FALSE;
  1281.   ubuffree (qinfo->zconfirm);
  1282.   qinfo->zconfirm = NULL;
  1283.  
  1284.   return fqueue_send (qdaemon, qtrans);
  1285. }
  1286.  
  1287. /* Add a line to the fake execution file.  */
  1288.  
  1289. static void
  1290. usadd_exec_line (pz, pcalc, pclen, bcmd, z1, z2)
  1291.      char **pz;
  1292.      size_t *pcalc;
  1293.      size_t *pclen;
  1294.      int bcmd;
  1295.      const char *z1;
  1296.      const char *z2;
  1297. {
  1298.   size_t c1, c2;
  1299.   char *znew;
  1300.  
  1301.   c1 = strlen (z1);
  1302.   c2 = strlen (z2);
  1303.  
  1304.   if (*pclen + c1 + c2 + 4 >= *pcalc)
  1305.     {
  1306.       *pcalc += c1 + c2 + 100;
  1307.       znew = zbufalc (*pcalc);
  1308.       if (*pclen > 0)
  1309.     {
  1310.       memcpy (znew, *pz, *pclen);
  1311.       ubuffree (*pz);
  1312.     }
  1313.       *pz = znew;
  1314.     }
  1315.  
  1316.   znew = *pz + *pclen;
  1317.   *znew++ = bcmd;
  1318.   if (*z1 != '\0')
  1319.     {
  1320.       *znew++ = ' ';
  1321.       memcpy (znew, z1, c1);
  1322.       znew += c1;
  1323.       if (*z2 != '\0')
  1324.     {
  1325.       *znew++ = ' ';
  1326.       memcpy (znew, z2, c2);
  1327.       znew += c2;
  1328.     }
  1329.     }
  1330.  
  1331.   /* In some bizarre non-Unix case we might have to worry about the
  1332.      newline here.  We don't know how a newline is normally written
  1333.      out to a file, but whatever is written to a file is what we will
  1334.      normally transfer.  If that is not simply \n then this fake
  1335.      execution file will not look like other execution files.  */
  1336.   *znew++ = '\n';
  1337.  
  1338.   *pclen = znew - *pz;
  1339. }
  1340.  
  1341. /* This routine is called to send the contents of the fake execution
  1342.    file.  Normally file data is sent by the floop routine in trans.c,
  1343.    but since we don't have an actual file we must do it here.  This
  1344.    routine sends the complete buffer, followed by a zero length
  1345.    packet, and then calls fsend_file_end.  */
  1346.  
  1347. static boolean
  1348. fsend_exec_file (qtrans, qdaemon)
  1349.      struct stransfer *qtrans;
  1350.      struct sdaemon *qdaemon;
  1351. {
  1352.   struct ssendinfo *qinfo = (struct ssendinfo *) qtrans->pinfo;
  1353.   char *zdata;
  1354.   size_t cdata;
  1355.   size_t csend;
  1356.  
  1357.   zdata = (*qdaemon->qproto->pzgetspace) (qdaemon, &cdata);
  1358.   if (zdata == NULL)
  1359.     {
  1360.       usfree_send (qtrans);
  1361.       return FALSE;
  1362.     }
  1363.  
  1364.   csend = qinfo->cbytes - qtrans->ipos;
  1365.   if (csend > cdata)
  1366.     csend = cdata;
  1367.  
  1368.   memcpy (zdata, qinfo->zexec + qtrans->ipos, csend);
  1369.  
  1370.   if (! (*qdaemon->qproto->pfsenddata) (qdaemon, zdata, csend,
  1371.                     qtrans->ilocal, qtrans->iremote,
  1372.                     qtrans->ipos))
  1373.     {
  1374.       usfree_send (qtrans);
  1375.       return FALSE;
  1376.     }
  1377.  
  1378.   qtrans->cbytes += csend;
  1379.   qtrans->ipos += csend;
  1380.  
  1381.   if (csend == 0)
  1382.     return fsend_file_end (qtrans, qdaemon);
  1383.  
  1384.   /* Leave the job on the send queue.  */
  1385.  
  1386.   return TRUE;
  1387. }
  1388.